import loadingUrl from "@/assets/loading.svg"
import styles from "./loading.module.less"
//导出指令的配置对象
function getLoadingImg(el){
    return el.querySelector("img[data-role=loading")
}

function createLoadingImg(){
    const img = document.createElement("img");
    img.dataset.role = "loading";
    img.src=loadingUrl;
    img.className = styles.loading;
    return img;
}

export default function (el, binding) {
    //根据bingding.value 的值决定创建删除img元素
    const curImg = getLoadingImg(el);
    if(binding.value) {
        if(!curImg){
            const img = createLoadingImg();
            el.appendChild(img);
        }
    }else {
        //删除img
        if(curImg){
            curImg.remove()
        }
    }
}